/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.io.IOException;
import java.util.Map;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.morphiastic.analysis.AnalysisComponentDefinition;
import com.morphiastic.utils.EqualsUtil;


/**
 * A phonetic token filter that can be configured with different encoder types:
 * metaphone, soundex, caverphone, refined_soundex, double_metaphone (uses
 * commons codec).
 * 
 * The replace parameter (defaults to true) controls if the token processed
 * should be replaced with the encoded one (set it to true), or added (set it to
 * false).
 * 
 * @author Alex
 * 
 */
public class PhoneticTokenFilter extends ElasticFilter{

	
	public static final String ENCODER_PARAM_KEY = "encoder";
	public static final String REPLACE_PARAM_KEY = "replace";
	
	public static enum Encoder{
		metaphone, soundex, caverphone, refined_soundex, double_metaphone
	}
	
	
	//I know this SUCKS but Java annotations dont like enum.toString ... 
	public static final String metaphone = "metaphone";
	public static final String soundex = "soundex";
	public static final String caverphone = "caverphone";
	public static final String refined_soundex = "refined_soundex";
	public static final String double_metaphone = "double_metaphone";
	
	
	
	
	
	private Encoder encoder;
	private Boolean replace;
	
	

	public PhoneticTokenFilter(String defName){
		this(null,defName);
	}
	
	public PhoneticTokenFilter(Map<String,String> params, String defName){
		
		super(ElasticFilterType.phonetic, defName);
		
		if(params!=null){
			
			String encoderParam = params.get(ENCODER_PARAM_KEY);
			if(encoderParam != null && !encoderParam.isEmpty()){
				try{
					this.encoder = Encoder.valueOf(encoderParam);
				}catch (Exception e) {
				}				
			}
			
			String replaceParam = params.get(REPLACE_PARAM_KEY);
			if(replaceParam!=null && !replaceParam.isEmpty()){
				this.replace = Boolean.parseBoolean(replaceParam);
			}
		}
	}
	
	@Override
	public boolean hasCustomParams() {
		 if(this.encoder!= null || 
			this.replace != null
		 )return true;
		 return false;
	}
	
	
	

	@Override
	public XContentBuilder addToBuilder(XContentBuilder mappingBuilder) throws IOException {
		mappingBuilder.startObject(getDefinitionName())
		.field("type", getComponentSubTypeName());
		
		if(this.encoder != null){
			mappingBuilder.field(ENCODER_PARAM_KEY, this.encoder.toString());			
		}
		if(this.replace != null){
			mappingBuilder.field(REPLACE_PARAM_KEY, this.replace.booleanValue());			
		}
		
		return mappingBuilder;
	}
	
	
	

	@Override
	public void setParamsFromSettings(Settings settings) {
		
		String encoderParam = settings.get(ENCODER_PARAM_KEY);
		if(encoderParam != null && !encoderParam.isEmpty()){
			try{
				this.encoder = Encoder.valueOf(encoderParam);
			}catch (Exception e) {
			}				
		}
		
		String replaceParam = settings.get(REPLACE_PARAM_KEY);
		if(replaceParam!=null && !replaceParam.isEmpty()){
			this.replace = Boolean.parseBoolean(replaceParam);
		}
	}
	
	
	
	

	@Override
	public boolean areEquivalentDefinitions(AnalysisComponentDefinition other){
		
		if(other==null)return false;
		PhoneticTokenFilter o;
		try {
			o = (PhoneticTokenFilter)other;
		} catch (ClassCastException e) {
			return false;
		}
		
		return 
		EqualsUtil.areEqual(this.encoder, o.encoder)
		&&
		EqualsUtil.areEqual(this.replace, o.replace);
	}
	
	
	
	
	
	
	
}
